<?php
namespace Modules\Api\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Collect;
use App\Models\Goods;
use App\Models\GoodsClass;
use App\Models\GoodsColor;
use App\Models\GoodsSku;
use App\Models\OrderEvaluate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class GoodsController extends Controller{

    //商品分类
    public function goodsClassList(){
        try{
            $class = GoodsClass::select('id','class_name')->get();
            return self::success(["list"=>$class]);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //商品列表
    public function goodsList(Request $request){
        try{
            $classId = $request->input("class_id",'all');
            $page = $request->input("page",1);
            $limit = $request->input("size",20);
            $goodsName = $request->input("goods_name",false);
            $query = Goods::query();
            if ($classId!='all'){
                $classData = GoodsClass::find($classId);
                if (!$classData)return self::error(1003);
                $query->where("class_id",$classId);
            }
            if ($goodsName){
                $query->where("goods_name","like","%{$goodsName}%");
            }
            $count = $query->count();
            $goods = $query->skip(($page-1)*$limit)->take($limit)->orderBy("id","desc")->select("id","goods_name","image","price","integral","is_free_shopping","detail","read_num")->get();
            return self::success(["count"=>$count,"list"=>$goods]);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //商品详情
    public function goodsDetail(Request $request){
        try{
            $goodsId = $request->input("goods_id",false);
            if (!$goodsId)return self::error(1000);
            $goods = Goods::find($goodsId);
            if (!$goods)return self::error(1005);
            $sku = GoodsSku::where("goods_id",$goodsId)->get();
            $color = GoodsColor::where("goods_id",$goodsId)->get();
            $collect = Collect::where("goods_id",$goodsId)->count();
            return self::success(["goods"=>$goods,"sku"=>$sku,"color"=>$color,"collect"=>$collect]);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //商品评价
    public function goodsEavluate(Request $request){
        try{
            $goodsId = $request->input("id",false);
            $page = $request->input("page",1);
            $size = $request->input("size",20);
            if (!$goodsId)return self::error(1000);
            $goods = Goods::find($goodsId);
            if (!$goods)return self::error(1005);
            $count = OrderEvaluate::where("goods_id",$goodsId)->count();
            $pingja = OrderEvaluate::where("goods_id",$goodsId)->forPage($page,$size)->orderBy("id","desc")->with('user')->get();
            if ($pingja->count()<1)return self::error(1003);
            $array = [];
            foreach ($pingja as $item){
                $array[] = [
                    "id"=>$item['id'],
                    "nickname"=>$item['user']['nickname'],
                    "avatar"=>$item['user']['avatar'],
                    "content"=>$item['content'],
                    "image"=>$item['image'],
                    "star"=>$item['star'],
                    "read"=>$item['read'],
                    "created_at"=>date('Y-m-d H:i:s',strtotime($item['created_at']))
                ];
            }
            return self::success(["count"=>$count,"data"=>$array]);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }
}